package org.htmlparser.tags;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.htmlparser.Attribute;
import org.htmlparser.Node;
import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.SimpleNodeIterator;

public class AppletTag extends CompositeTag
{
  private static final String[] mEndTagEnders;
  private static final String[] mIds;

  static
  {
    String[] arrayOfString1 = new String[1];
    arrayOfString1[0] = "APPLET";
    mIds = arrayOfString1;
    String[] arrayOfString2 = new String[2];
    arrayOfString2[0] = "BODY";
    arrayOfString2[1] = "HTML";
    mEndTagEnders = arrayOfString2;
  }

  public Hashtable createAppletParamsTable()
  {
    Hashtable localHashtable = new Hashtable();
    NodeList localNodeList = getChildren();
    if (localNodeList != null)
      for (int i = 0; i < localNodeList.size(); ++i)
      {
        Node localNode = this.children.elementAt(i);
        if (localNode instanceof Tag)
        {
          Tag localTag = (Tag)localNode;
          if (localTag.getTagName().equals("PARAM"))
          {
            String str = localTag.getAttribute("NAME");
            if ((str != null) && (str.length() != 0))
              localHashtable.put(str, localTag.getAttribute("VALUE"));
          }
        }
      }
    return localHashtable;
  }

  public String getAppletClass()
  {
    return getAttribute("CODE");
  }

  public Hashtable getAppletParams()
  {
    return createAppletParamsTable();
  }

  public String getArchive()
  {
    return getAttribute("ARCHIVE");
  }

  public String getCodeBase()
  {
    return getAttribute("CODEBASE");
  }

  public String[] getEndTagEnders()
  {
    return mEndTagEnders;
  }

  public String[] getIds()
  {
    return mIds;
  }

  public String getParameter(String paramString)
  {
    return ((String)(String)getAppletParams().get(paramString));
  }

  public Enumeration getParameterNames()
  {
    return getAppletParams().keys();
  }

  public void setAppletClass(String paramString)
  {
    setAttribute("CODE", paramString);
  }

  public void setAppletParams(Hashtable paramHashtable)
  {
    NodeList localNodeList = getChildren();
    if (localNodeList == null)
    {
      localNodeList = new NodeList();
      Enumeration localEnumeration = paramHashtable.keys();
      while (true)
      {
        if (!(localEnumeration.hasMoreElements()))
          break label279;
        Vector localVector = new Vector();
        String str1 = (String)localEnumeration.nextElement();
        String str2 = (String)paramHashtable.get(str1);
        localVector.addElement(new Attribute("PARAM", null));
        localVector.addElement(new Attribute(" "));
        localVector.addElement(new Attribute("VALUE", str2, '"'));
        localVector.addElement(new Attribute(" "));
        localVector.addElement(new Attribute("NAME", str1, '"'));
        localNodeList.add(new TagNode(null, 0, 0, localVector));
      }
    }
    int i = 0;
    while (true)
    {
      while (true)
      {
        while (true)
        {
          Node localNode2;
          do
          {
            do
            {
              if (i >= localNodeList.size());
              Node localNode1 = localNodeList.elementAt(i);
              if (!(localNode1 instanceof Tag))
                break label273;
              if (!(((Tag)localNode1).getTagName().equals("PARAM")))
                break label267;
              localNodeList.remove(i);
            }
            while (i >= localNodeList.size());
            localNode2 = localNodeList.elementAt(i);
          }
          while ((!(localNode2 instanceof Text)) || (((Text)localNode2).getText().trim().length() != 0));
          localNodeList.remove(i);
        }
        label267: ++i;
      }
      label273: ++i;
    }
    label279: setChildren(localNodeList);
  }

  public void setArchive(String paramString)
  {
    setAttribute("ARCHIVE", paramString);
  }

  public void setCodeBase(String paramString)
  {
    setAttribute("CODEBASE", paramString);
  }

  public String toString()
  {
    int j;
    Node localNode;
    StringBuffer localStringBuffer = new StringBuffer(500);
    localStringBuffer.append("Applet Tag\n");
    localStringBuffer.append("**********\n");
    localStringBuffer.append("Class Name = ");
    localStringBuffer.append(getAppletClass());
    localStringBuffer.append("\n");
    localStringBuffer.append("Archive = ");
    localStringBuffer.append(getArchive());
    localStringBuffer.append("\n");
    localStringBuffer.append("Codebase = ");
    localStringBuffer.append(getCodeBase());
    localStringBuffer.append("\n");
    Hashtable localHashtable = getAppletParams();
    Enumeration localEnumeration = localHashtable.keys();
    if (localEnumeration == null)
    {
      localStringBuffer.append("No Params found.\n");
      j = 0;
      SimpleNodeIterator localSimpleNodeIterator = children();
      do
      {
        if (!(localSimpleNodeIterator.hasMoreNodes()))
          break label296;
        localNode = localSimpleNodeIterator.nextNode();
      }
      while ((localNode instanceof Tag) && (((Tag)localNode).getTagName().equals("PARAM")));
      if (j != 0)
        break label286;
      localStringBuffer.append("Miscellaneous items :\n");
    }
    while (true)
    {
      while (true)
      {
        j = 1;
        localStringBuffer.append(localNode.toString());
      }
      int i = 0;
      while (true)
      {
        if (!(localEnumeration.hasMoreElements()));
        String str1 = (String)localEnumeration.nextElement();
        String str2 = (String)localHashtable.get(str1);
        localStringBuffer.append(i);
        localStringBuffer.append(": Parameter name = ");
        localStringBuffer.append(str1);
        localStringBuffer.append(", Parameter value = ");
        localStringBuffer.append(str2);
        localStringBuffer.append("\n");
        ++i;
      }
      label286: localStringBuffer.append(" ");
    }
    if (j != 0)
      label296: localStringBuffer.append("\n");
    localStringBuffer.append("End of Applet Tag\n");
    localStringBuffer.append("*****************\n");
    return localStringBuffer.toString();
  }
}